package fragment.contact;

import android.text.TextUtils;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.util.LogUtils;
import com.socks.library.KLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import application.AppApplication;
import application.HttpUrlConstant;
import menhoo.com.i8officelibrary.R;
import model.EmployeeItemDBModel;
import model.EmployeeResultDB;

/**
 * Created by ThinkPad on 2017/5/22.
 */

public class ContactsModel {

    public ContactsModel() {

    }

    public static String[] indexChar = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "*"};


    private String nowDeptID = null;
    private Stack<String> stackDeptID = new Stack<String>();

    /**
     * 保存此次获取list的id记录
     *
     * @param deptID
     */
    public void setDeptID(String deptID) {
        if (!TextUtils.isEmpty(nowDeptID)) {
            stackDeptID.push(nowDeptID);
        }
        nowDeptID = deptID;
    }

    public String getNowDeptID() {
        return nowDeptID;
    }

    /**
     * 获取上次的id记录 没有则返回null
     *
     * @return
     */
    public String backDeptID() {
        String result;
        if (stackDeptID.empty()) {
            result = null;
        } else {
            result = stackDeptID.pop();
            nowDeptID = result;
        }
        return result;
    }

    public List<EmployeeItemDBModel> getEmployeeItemDBModelsFromNomal(boolean haveHead, JSONObject jsonResult) throws JSONException {
        List<EmployeeItemDBModel> listData = new ArrayList<EmployeeItemDBModel>();
        //2018.5.16 去掉群聊选项
//        if (haveHead) {
//
//            EmployeeItemDBModel item = new EmployeeItemDBModel();
//            item.DeptID = "chat";
//            item.EmplID = null;
//            item.Name = "群聊";
//            item.type = EmployeeItemDBModel.ITEM;
//            listData.add(item);
////            item = new EmployeeItemDBModel();
////            item.DeptID = "all";
////            item.EmplID = null;
////            item.Name = "所有人";
////            item.type = EmployeeItemDBModel.ITEM;
////            listData.add(item);
//        }
        String CurrentDeptName = jsonResult.getString("CurrentDeptName");
        JSONArray Depts = jsonResult.getJSONArray("Depts");
        JSONArray Empls = jsonResult.getJSONArray("Empls");

        if (Depts.length() > 0) {
            listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, "部门"));
        }
        for (int i = 0; i < Depts.length(); i++) {
            JSONObject deptObj = Depts.getJSONObject(i);
            String ID = deptObj.getString("ID");
            String Name = deptObj.getString("Name");

            EmployeeItemDBModel item = new EmployeeItemDBModel();
            item.DeptID = ID;
            item.EmplID = null;
            item.Name = Name;
            item.type = EmployeeItemDBModel.ITEM;
            listData.add(item);
        }
        if (Empls.length() > 0) {
            listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, "人员"));
        }
        for (int i = 0; i < Empls.length(); i++) {
            JSONObject emplObj = Empls.getJSONObject(i);
            String ID = emplObj.getString("ID");
            String Name = emplObj.getString("Name");
            String DeptID = emplObj.getString("DeptID");
            String Sex = emplObj.getString("Sex");

            EmployeeItemDBModel item = new EmployeeItemDBModel();
            item.EmplID = ID;
            item.DeptID = null;
            item.Name = Name;
            item.type = EmployeeItemDBModel.ITEM;

            for (EmployeeItemDBModel model : peopleCheckList) {
                if (ID.equals(model.EmplID) && Name.equals(model.Name)) {
                    item.checked = true;
                }
            }

//                if (!ID.equals(AppApplication.getUser().EmplID)){
            listData.add(item);
//                }


        }

        return listData;
    }

    public List<EmployeeItemDBModel> getEmployeeItemDBModelsFromSearch(JSONObject jsonResult) {
        List<EmployeeItemDBModel> listData = new ArrayList<>();
        listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, "#"));
        JSONArray jsonDataArr = jsonResult.optJSONArray("Data");
        for (int i = 0; i < jsonDataArr.length(); i++) {
            JSONObject jsonItem = jsonDataArr.optJSONObject(i);
            String ID = jsonItem.optString("ID");
            String Name = jsonItem.optString("Name");
            String DeptID = jsonItem.optString("DeptID");
            String DeptName = jsonItem.optString("DeptName");
            String DeptAndPosStr = jsonItem.optString("DeptAndPosStr");
            String LoginName = jsonItem.optString("LoginName");

            EmployeeItemDBModel item = new EmployeeItemDBModel(ID, Name, DeptID, DeptName, DeptAndPosStr, LoginName);
            item.type = EmployeeItemDBModel.ITEM;
            for (EmployeeItemDBModel model : peopleCheckList) {
                if (ID.equals(model.EmplID) && Name.equals(model.Name)) {
                    item.checked = true;
                }
            }
            listData.add(item);

        }
        return listData;
    }

    public List<EmployeeItemDBModel> getEmployeeItemDBModelsFromAll(JSONObject jsonResult) throws JSONException {

        List<EmployeeItemDBModel> listData = new ArrayList<EmployeeItemDBModel>();
        final int count = jsonResult.optInt("Count");
        JSONObject jsonData = jsonResult.getJSONObject("Data");
        for (int j = 0; j < indexChar.length; j++) {
            String index_t = indexChar[j];
            if (!jsonData.isNull(index_t)) {
                listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, index_t));
                JSONArray jsonDataArr = jsonData.getJSONArray(index_t);
                for (int i = 0; i < jsonDataArr.length(); i++) {
                    JSONObject jsonItem = jsonDataArr.optJSONObject(i);
                    String ID = jsonItem.optString("ID");
                    String Name = jsonItem.optString("Name");
                    String DeptID = jsonItem.optString("DeptID");
                    String DeptName = jsonItem.optString("DeptName");
                    String DeptAndPosStr = jsonItem.optString("DeptAndPosStr");
                    String LoginName = jsonItem.optString("LoginName");

                    EmployeeItemDBModel item = new EmployeeItemDBModel(ID, Name, DeptID, DeptName, DeptAndPosStr, LoginName);
                    item.type = EmployeeItemDBModel.ITEM;

                    for (EmployeeItemDBModel model : peopleCheckList) {
                        if (ID.equals(model.EmplID) && Name.equals(model.Name)) {
                            item.checked = true;
                        }
                    }
//                    if (!ID.equals(AppApplication.getUser().EmplID)){
//                        listData.add(item);
//                    }
                    listData.add(item);

                }
            }
        }
        return listData;
    }


    /**
     * listView的数据
     */
    private List<EmployeeItemDBModel> peopleCheckList = new ArrayList<>();
    private List<String> peopleCheckIDList = new ArrayList<>();

    public void setSelectEmpl(EmployeeItemDBModel item) {
//        if(item.checked){
//            ListDataSelect.put(item.EmplID,item);
//        }else{
//            ListDataSelect.remove(item.EmplID);
//        }
//        Set<String> key = ListDataSelect.keySet();

        if (item.checked) {
            if (!peopleCheckIDList.contains(item.EmplID)) {
                peopleCheckList.add(item);
                peopleCheckIDList.add(item.EmplID);
            }

        } else {
            if (peopleCheckIDList.contains(item.EmplID)) {
                int index = peopleCheckIDList.indexOf(item.EmplID);
                peopleCheckList.remove(index);
                peopleCheckIDList.remove(index);


            }
        }

        LogUtils.e("选中的数目：" + peopleCheckList.size());
    }

    public List<EmployeeItemDBModel> getPeopleCheckList() {
        return peopleCheckList;
    }
}
